/*
 * This file is part of Cadmium.
 * Copyright (C) 2007-2010 Xavier Clerc.
 *
 * Cadmium is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 *
 * Cadmium is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package fr.x9c.cadmium.primitives.graph;

import java.awt.image.BufferedImage;

import fr.x9c.cadmium.kernel.Block;
import fr.x9c.cadmium.kernel.CodeRunner;
import fr.x9c.cadmium.kernel.Fail;
import fr.x9c.cadmium.kernel.Primitive;
import fr.x9c.cadmium.kernel.PrimitiveProvider;
import fr.x9c.cadmium.kernel.Value;

/**
 * This class provides the implementation of 'caml_gr_make_image' primitive.
 *
 * @author <a href="mailto:cadmium@x9c.fr">Xavier Clerc</a>
 * @version 1.0
 * @since 1.0
 */
@PrimitiveProvider
public final class MakeImage {

    /** Integer value for transparent pixel. */
    private static final int TRANSPARENT = -1;

    /**
     * No instance of this class.
     */
    private MakeImage() {
    } // end empty constructor

    /**
     * Creates an image. from a pixel array.
     * @param ctxt context
     * @param m pixel array
     * @return image created from pixel array
     * @throws Fail.Exception if all subarrays do not have the same length
     */
    @Primitive
    public static Value caml_gr_make_image(final CodeRunner ctxt,
                                           final Value m)
        throws Fail.Exception {
        GraphSlot.checkGraph(ctxt);
        final Block data = m.asBlock();
        final int height = data.getWoSize();
        if (height == 0) {
            final Block res = Block.createCustom(CustomImage.SIZE,
                                                 CustomImage.OPS);
            res.setCustom(new BufferedImage(0, 0, BufferedImage.TYPE_INT_ARGB));
            return Value.createFromBlock(res);
        } // end if
        final int width = data.get(0).asBlock().getWoSize();
        for (int i = 1; i < height; i++) {
            if (data.get(i).asBlock().getWoSize() != width) {
                GraphSlot.fail(ctxt, "make_image: lines of different lengths", "");
            } // end if
        } // end for
        final int[] pixels = new int[height * width];
        for (int i = 0; i < height; i++) {
            final Block line = data.get(i).asBlock();
            for (int j = 0; j < width; j++) {
                final int rgb = line.get(j).asLong();
                if (rgb == MakeImage.TRANSPARENT) {
                    pixels[i * width + j] = 0;
                } else {
                    pixels[i * width + j] = rgb | 0xFF000000;
                } // end if/else
            } // end for
        } // end for

        final BufferedImage img =
            new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
        img.setRGB(0, 0, width, height, pixels, 0, width);
        final Block res = Block.createCustom(CustomImage.SIZE,
                                             CustomImage.OPS);
        res.setCustom(img);
        return Value.createFromBlock(res);
    } // end method 'caml_gr_make_image(CodeRunner, Value)'

} // end class 'MakeImage'
